<?php

namespace app\shopapi\controller;

use app\shopapi\model\User as UserModel;

/**
 * 用户管理
 * Class User
 * @package app\api
 */
class User extends Controller
{
    /**
     * 用户自动登录
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function login()
    {
        $model = new UserModel;
        return $this->renderSuccess([
            'user_id' => $model->login($this->request->post()),
            'token' => $model->getToken()
        ]);
    }

    /**
     * 当前用户详情
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        // 当前用户信息
        $userInfo = $this->getUser();
        return $this->renderSuccess(compact('userInfo'));
    }
     // 新增用户改昵称和头像
    public function updatenickname()
    {
        $userInfo = $this->getUser();
        $user_id = $userInfo['user_id'];
        $arr['nickName'] = input()['nickName'];
        if (isset(input()['avatarUrl'])) {
            $arr['avatarUrl'] = input()['avatarUrl'];
        }
        $model = new UserModel;
        $model->where("user_id", $user_id)->update($arr);
        return $this->renderSuccess([],"修改成功");
    }
    public function phone()
    {
        $userInfo = $this->getUser();
        $user_id = $userInfo['user_id'];
        $arr['phone'] = input()['phone'];
        $model = new UserModel;
        $model->where("user_id", $user_id)->update($arr);
        return $this->renderSuccess([],"修改成功");
    }

}
